package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.List;
import java.util.Objects;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowActivityJumpHistoryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowActivityJumpHistoryRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.event.ActivityJumpEvent;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowActivityJumpHistory;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowActivityJumpHistoryExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowActivityJumpHistoryDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.flowable.task.api.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-04-22 14:11
 */
@Service
public class FlowActivityJumpHistoryRepositoryImpl implements FlowActivityJumpHistoryRepository {

    @Autowired
    private FlowActivityJumpHistoryDAO flowActivityJumpHistoryDAO;

    @Override
    public int insertActivityJumpHistory(FlowActivityJumpHistoryVo vo) {
        FlowActivityJumpHistory entity = new FlowActivityJumpHistory();
        BeanUtils.copyProperties(vo, entity);
        return flowActivityJumpHistoryDAO.insertSelective(entity);
    }

    @Override
    public FlowActivityJumpHistoryVo getJumpHistoryByTaskId(String taskId) {
        FlowActivityJumpHistoryExample query = new FlowActivityJumpHistoryExample();
        query.createCriteria().andTaskIdEqualTo(taskId);
        List<FlowActivityJumpHistory> flowActivityJumpHistories = flowActivityJumpHistoryDAO.selectByExample(query);
        if (CollectionUtils.isEmpty(flowActivityJumpHistories)) {
            return null;
        }
        return convert(flowActivityJumpHistories.get(0));
    }


    @EventListener
    public void handleActivityJumpEvent(ActivityJumpEvent activityJumpEvent) {
        for (Task task : activityJumpEvent.getCurrentTasks()) {
            FlowActivityJumpHistoryVo entity = new FlowActivityJumpHistoryVo();
            entity.setCreateName(activityJumpEvent.getCreatorName());
            entity.setComment(activityJumpEvent.getComment());
            entity.setProcessInstanceId(activityJumpEvent.getProcessInstanceId());
            entity.setToActivityId(activityJumpEvent.getNewActivityId());
            entity.setToActivityName(activityJumpEvent.getNewActivityName());
            entity.setFromActivityId(task.getTaskDefinitionKey());
            entity.setFromActivityName(task.getName());
            entity.setTaskId(task.getId());
            insertActivityJumpHistory(entity);
        }
    }

    private FlowActivityJumpHistoryVo convert(FlowActivityJumpHistory entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowActivityJumpHistoryVo result = new FlowActivityJumpHistoryVo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }
}
